package com.weitu.occupationalHealth.hseService.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.occupationalHealth.hseService.entity.HseServiceOrg;
import com.weitu.occupationalHealth.hseService.service.HseServiceOrgService;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/4/1.
 */
@Controller
@RequestMapping(value = "occupationalHealth/hseServiceOrg")
public class HseServiceOrgController extends BaseController {
    @Autowired
    private HseServiceOrgService hseServiceOrgService;
    @Autowired
    private AccessoryUtilService accessoryUtilService;
    /**
     * 跳转至服务机构信息页面
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public String list() {
        return BASE_JSP_PATH + "occupationalHealth/hseServiceOrg/list";
    }

    /**
     * 跳转至服务机构新增页面
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public String add() {
        return BASE_JSP_PATH + "occupationalHealth/hseServiceOrg/add";
    }

    @RequestMapping(value = "/orgTb")
    public String orgTb() {
        return BASE_JSP_PATH + "occupationalHealth/hseServiceOrg/orgTb";
    }
    /**
     * 跳转至服务机构编辑页面
     *
     * @param id
     * @return
     */
    @RequestMapping("edit")
    public ModelAndView edit(String id) {
        ModelAndView modelAndView = new ModelAndView();
        Map hseServiceOrg=hseServiceOrgService.selectById(id);
        modelAndView.addObject("org", hseServiceOrg);
        modelAndView.setViewName(BASE_JSP_PATH + "occupationalHealth/hseServiceOrg/edit");
        return modelAndView;
    }
    /**
     * 跳转至服务机构信息页面
     *
     * @param id
     * @return
     */
    @RequestMapping("info")
    public ModelAndView info(String id,String type) {
        ModelAndView modelAndView = new ModelAndView();
        Map hseServiceOrg=hseServiceOrgService.selectById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("org", hseServiceOrg);
        if (type.equals("info")){
            modelAndView.setViewName(BASE_JSP_PATH + "occupationalHealth/hseServiceOrg/info");
        }else {
            modelAndView.setViewName(BASE_JSP_PATH + "occupationalHealth/hseServiceOrg/edit");
        }
        return modelAndView;
    }
    /**
     * 服务机构信息列表
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/serviceOrgTable", method = RequestMethod.POST)
    public Object serviceOrgTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);

        int totalNum = this.hseServiceOrgService.selectServiceOrgCount(paramMap);
        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Map> dataList = this.hseServiceOrgService.selectAllServiceOrg(paramMap);
        Object data = dataGridPagination.flushDataGridData(dataList);
        return data;
    }

    @ResponseBody
    @RequestMapping("insertSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "服务机构信息", methods = "添加服务机构")
    public AjaxResult insertSelective(HseServiceOrg hseServiceOrg) {
        String id=IDGenerator.getId();
        hseServiceOrg.setId(id);
        hseServiceOrg.setDelState(0);
        String time=DateTimeUtil.formatterDateTime();
        hseServiceOrg.setAddTime(time);
        User user =getSessionUser();
        String userId=user.getId();
        String userDeptId=user.getDepartment().getId();
        hseServiceOrg.setAddUserId(userId);
        hseServiceOrg.setAddDeptId(userDeptId);
        int boo=hseServiceOrgService.insertSelective(hseServiceOrg);
        if (boo > 0) {
            boolean accFlag= AccessoryUtil.saveAccerssoryInfo(hseServiceOrg.getId());
            if (accFlag){
                return success("添加成功");
            }else {
                return fail("添加失败");
            }
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 更新服务机构信息
     *
     * @param hseServiceOrg
     * @return
     */
    @ResponseBody
    @RequestMapping("updateByIdSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "服务机构", methods = "更新服务机构信息")
    public AjaxResult updateByIdSelective(HseServiceOrg hseServiceOrg) {
        String time = DateTimeUtil.formatterDateTime();
        hseServiceOrg.setUpdateTime(time);
        User loginUser = getSessionUser();
        String loginUserId = loginUser.getId();
        String loginUserDeptId = loginUser.getDepartment().getId();
        hseServiceOrg.setUpdateUserId(loginUserId);
        hseServiceOrg.setUpdateDeptId(loginUserDeptId);
        int boo = hseServiceOrgService.updateByIdSelective(hseServiceOrg);
        if (boo > 0) {
            boolean accFlag= AccessoryUtil.saveAccerssoryInfo(hseServiceOrg.getId());
            if (accFlag){
                return success("添加成功");
            }else {
                return fail("添加失败");
            }
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 删除服务机构
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "服务机构", methods = "删除服务机构")
    public AjaxResult deleteById(String id){
        int boo=hseServiceOrgService.deleteById(id);
        if (boo==1){
            return success("删除成功");
        }else {
            return fail("删除失败");
        }
    }
}
